﻿using System;
using System.Linq;
using System.Web.Mvc;
using XRERP.Models;
using System.Data.Entity;
using XRERP.utils;

namespace XRERP.Controllers
{
    /// <summary>
    /// 项目档案管理
    /// </summary>
    public class ProjectFilesController : Controller
    {
        private MySqlDB db = new MySqlDB();

        // GET: ProjectFiles
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult QueryProjectFiles(
            String QueryCustomerId="",
            String QueryProjectNameId="",
            String QueryProjectDateStart="",
            String QueryProjectDateEnd="",
            int page = 1, int rows = 10
            )
        {

            

            var data = from a in db.xr_bl_proarc
                       from b in db.xr_bl_clientinfo 
                       from c in db.xr_bl_proinfo 
                       where a.clientinfo_uuid==b.clientinfo_uuid
                       && a.proinfo_uuid==c.proinfo_uuid
                       select new
                       {
                           //a.pro_address,
                           //a.engineer_name,
                           a.clientinfo_uuid,
                           a.proinfo_uuid,
                           a.proarc_uuid,
                           a.procha_name,
                           a.inv_category,
                           a.proarc_number,
                           probegin_date= a.probegin_date.ToString(),
                           proend_date=a.proend_date.ToString(),
                           procheck_date=a.procheck_date.ToString(),
                           proreport_date=a.proreport_date.ToString(),
                           b.client_name,
                           c.pro_name
                       };

            if(!QueryCustomerId.Equals(""))
            {
                data= data.
                    Where(x=>x.client_name.Contains(QueryCustomerId));
            }

            if (!QueryProjectNameId.Equals(""))
            {
                data = data.
                    Where(x => x.pro_name.Contains(QueryProjectNameId));
            }

            if (!QueryProjectDateStart.Equals(""))
            {

                data = data.
                    Where(x => x.probegin_date.CompareTo(QueryProjectDateStart) >= 0);
            }

            if (!QueryProjectDateEnd.Equals(""))
            {
                data = data.
                    Where(x => x.proend_date != "" && x.proend_date.CompareTo(QueryProjectDateEnd) <= 0);
                   
            }



            var table = new
            {
                rows = data
                .OrderBy(y => y.proarc_number)
                .Skip(rows * (page - 1))
                .Take(rows)
                .ToList(),
                total = data.Count()
            };

            return Json(table,JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 查询客户信息
        /// </summary>
        /// <returns></returns>
        public ActionResult getCustomer()
        {
            Func<xr_bl_clientinfo, bool> where = x => x.validflag == "1";
            return Json(db.xr_bl_clientinfo.Where(where).
                Select(
                    u=> new
                    {
                        u.clientinfo_uuid,
                        u.client_name
                    }
                )
                .ToList());
        }

        /// <summary>
        /// 查询项目信息
        /// </summary>
        /// <returns></returns>
        public ActionResult getProject(String uuid)
        {

            Func<xr_bl_proinfo, bool> where = x => x.validflag == "1";
            Func<xr_bl_proinfo, bool> where2 = x => x.clientinfo_uuid == uuid;

            return Json(db.xr_bl_proinfo.
                Where(where).
                Where(where2).
                Select(u=> new {
                    u.proinfo_uuid,
                    u.pro_name
                }).
                ToList());
        }


        /// <summary>
        /// 添加新的项目信息
        /// </summary>
        /// <param name="proarc"></param>
        /// <returns></returns>
        public ActionResult AddProjectFile(xr_bl_proarc proarc)
        {
            DateTime date = DateTime.Now;
            String year=date.Year.ToString();
            JR json = new JR();

            try
            {
                var data = db.xr_dl_numbermng.
                    Where(x => x.number_year == year).
                    Where(x => x.number_type == "3");
                var number =  data.Count()==0 ? null : data.First().mng_number;
                var firstdata = data.Count() == 0 ? new xr_dl_numbermng() : data.First();

                proarc.proarc_uuid = Tools.getGuid();
                proarc.validflag = "1";

                if (null == number || number.Equals(""))
                {
                    proarc.proarc_number = "P" + year + "00000001";
                    number = "1";
                }
                else
                {
                    int tmp = Convert.ToInt32(number) + 1;
                    number = tmp.ToString();
                    proarc.proarc_number = "P" + year + Tools.getNumber(number);
                }
                db.xr_bl_proarc.Add(proarc);
                if (db.SaveChanges() > 0)
                {
                    json.success = true;
                    json.code = 200;
                    json.message = "添加成功!项目档案编号：" + proarc.proarc_number;

                    firstdata.mng_number = number;
                    if (data.Count() == 0)
                    {
                        firstdata.number_uuid = Tools.getGuid();
                        firstdata.number_year = year;
                        firstdata.number_type = "3";
                        db.xr_dl_numbermng.Add(firstdata);
                    }
                    else
                    {
                        db.Entry(firstdata).State = EntityState.Modified;
                    }
                    
                    db.SaveChanges();

                }
                else
                {
                    json.code = 300;
                    json.message = "修改失败，请重试或与管理员联系！";
                    json.success = false;
                }
            } catch (Exception e)
            {
                json.code = 505;
                json.message = e.ToString();
                json.success = false;
            }
            

            return Json(json);
        }
    }
}